#!/usr/bin/ruby

require 'rbconfig'

####################################################################################################
# Each time a file is added or removed, the script must be re-run and a new project file generated.#
####################################################################################################

base_dir = ENV['PROC_MOD_BASE_DIR']
if base_dir == nil then puts 'Failure: PROC_MOD_BASE_DIR is not set'; exit end

sys_name = ''
rm_string = ''
case RbConfig::CONFIG['host_os']
when /linux/i
  sys_name = 'linux'
  rm_string = 'rm '
when /darwin/i
  sys_name = 'mac'
  rm_string = 'rm '
end

if sys_name == "linux"
  ld_lib_path = ENV['LD_LIBRARY_PATH']
  if not ld_lib_path.include?(base_dir+'Common/lib')
    puts 'Failure LD_LIBRARY_PATH does not contain ' + base_dir+'Common/lib'
    exit
  end
end

project_file_name='ProcModeler.pro'
version='0.1'

##########################
# Cleanup Existing Files #
##########################
if sys_name == 'linux' or sys_name == 'mac'
  if File.exists?(project_file_name) then system(rm_string + project_file_name) end
  if File.exists?(project_file_name + '.tmp') then system(rm_string + project_file_name + '.tmp') end
  if File.exists?('Makefile') then system(rm_string + 'Makefile') end
  if File.exists?('Makefile.Debug') then system(rm_string + 'Makefile.Debug') end
  if File.exists?('Makefile.Release') then system(rm_string + 'Makefile.Release') end
  if sys_name == 'mac'
    system('rm -rf *.xc*')
    system('rm *.plist')
    system('rm moc*.*pp')
    system('rm -rf *.app')
    system('rm -rf debug')
    system('rm -rf release')
    system('rm -rf build')
  end
end

###############################
### Generate initial PRO file #
###############################
system('qmake -project -nopwd src -o %s.tmp'%project_file_name)

#########################
# Create final PRO file #
#########################
initial_file = File.open(project_file_name + '.tmp','r')
final_file = File.open(project_file_name,'w')

initial_file.each_line do |line|
  if /^TEMPLATE.+/ =~ line
    final_file.puts('QT += core opengl')
    final_file.puts('CONFIG += qt debug_and_release build_all')
    final_file.puts('INCLUDEPATH += ../Common/include')
    final_file.puts('QMAKE_LIBDIR += ../Common/lib')
    final_file.puts('VERSION = %s'%version)
    final_file.puts('DESTDIR = .')

    if(sys_name == 'linux')
      final_file.puts('LIBS += -lGLEW')
      final_file.puts('TEMPLATE = app')
        final_file.puts('QMAKE_CXXFLAGS_RELEASE+= -O3') 
    elsif(sys_name == 'mac')
      final_file.puts('INCLUDEPATH += /usr/local/include /usr/include /opt/local/include')
      final_file.puts('LIBS += -lGLEW -L/usr/lib/ -L/usr/local/lib -L/opt/local/lib')
      final_file.puts('QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6')
      final_file.puts('CONFIG -= app_bundle')
      final_file.puts('TEMPLATE = app')
      final_file.puts('QMAKE_CXXFLAGS_RELEASE+= -O3') 
    end
  elsif /^TARGET.+/ =~ line
    final_file.puts('CONFIG(debug, debug|release) {')
    final_file.puts('TARGET = $$join(TARGET,,,_d)')
    final_file.puts('LIBS += -lProcModel_d')
    final_file.puts('}')
    final_file.puts('CONFIG(release, debug|release) { LIBS += -lProcModel }')
  else
    final_file.puts(line)
  end
end

initial_file.close

final_file.close

system(rm_string + '%s.tmp'%project_file_name)

########################################
# Generate build files                 #
########################################
system('qmake -makefile ' + project_file_name)
